﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using XcoAppSpaces.Contracts.Transport;
using XcoAppSpaces.Serialization.Binary;
using System.Diagnostics;
using XcoAppSpaces.Transport.MSMQ;

namespace test.XcoAppSpaces.Transport.MSMQ
{
    class ReceivedSendBack
    {
        public DateTime StartedAt { get; set; }
        private XcoMSMQTransportService comm2;
        private XcoBinarySerializer serializer = new XcoBinarySerializer();
        private string msmqName1;
        public ReceivedSendBack(XcoMSMQTransportService comm2, string msmqName1)
        {
            this.comm2 = comm2;
           this.msmqName1 = msmqName1;
         
        }
       
        public void MessageReceived(XcoMessage msg, string remoteMessage, IXcoTransportService commService)
        {

            IntMsg n = (IntMsg)serializer.Deserialize(msg.Content);
           
            if (n.value % 1000 == 0)
            {
                byte[] data = serializer.Serialize(new IntMsg {  value = n.value, strValue1 = n.value.ToString(), strValue2 = "resturn", msgPartList = new List<MsgPart> { new MsgPart { exValue = new Exception(), strValue = "res" } } });

				comm2.Send(new XcoMessage { Content = data }, msmqName1);

                Console.WriteLine("Worker 2: {0} @ {1}", n.value, DateTime.Now.Subtract(StartedAt).TotalSeconds);
            }  

        }
    }

    class ReceivedConsoleOut
    {
		private XcoBinarySerializer ser1 = new XcoBinarySerializer();
        
        public void MessageReceived(XcoMessage msg, string remoteAddress, IXcoTransportService commService)
        {
            IntMsg n = (IntMsg)ser1.Deserialize(msg.Content);
            Console.WriteLine("Worker 1: received result {0}", n.value);
        }
    }

  
    [Serializable]
    public class IntMsg
    {
        public int value;
        public string strValue1;
        public string strValue2;
        public List<MsgPart> msgPartList;
    }

    [Serializable]
    public class MsgPart
    {
        public string strValue;
        public Exception exValue;
    }
}
